package com.dcits.electronic_account.controller;

import com.alibaba.fastjson.JSON;
import com.dcits.electronic_account.bean.Flow;
import com.dcits.electronic_account.bean.ResponseJson;
import com.dcits.electronic_account.bean.VirtualAccount;
import com.dcits.electronic_account.constant.Constant;
import com.dcits.electronic_account.service.TransferAccountsService;
import com.dcits.electronic_account.utils.MD5Util;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: YHC
 * @Date: 2022/6/25
 */
@RequestMapping("/transfer")
@RestController
public class TransferAccountsController {

    @Resource
    TransferAccountsService transferAccountsService;

    @RequestMapping("/transByPhone")
    ResponseJson TransferByPhone(@RequestBody String data){
        //解析前端传来的信息
        Integer payerId = Integer.valueOf(JSON.parseObject(data).get("payerId").toString());
        String payeePhone = JSON.parseObject(data).get("payeePhone").toString();
        String payPwd = JSON.parseObject(data).get("payPwd").toString();
        BigDecimal amount = new BigDecimal(JSON.parseObject(data).get("amount").toString());

        VirtualAccount payer = transferAccountsService.searchVirById(payerId);
        VirtualAccount payee = transferAccountsService.searchVirByPhone(payeePhone);

        //先进行用户信息判定
        if(payer==null) return new ResponseJson(Constant.FAIL_CODE,"当前账户异常（不存在）");
        if(payee==null) return new ResponseJson(Constant.FAIL_CODE,"对方账号不存在");
        if(payee.getPhone().equals(payer.getPhone())) return new ResponseJson(Constant.FAIL_CODE,"不可以向自己账户转账");

        String checkPayPwd = payer.getPayPwd();
        BigDecimal checkAmount = payer.getBalance();

        if(!MD5Util.getMd5(payPwd).equals(checkPayPwd)) return new ResponseJson(Constant.FAIL_CODE,"支付密码有误");
        if(amount.compareTo(BigDecimal.ZERO)<=0) return new ResponseJson(Constant.FAIL_CODE,"金额输入有误");
        if(checkAmount.subtract(amount).compareTo(BigDecimal.ZERO)<0) return new ResponseJson(Constant.FAIL_CODE,"余额不足");

        //信息无误，开始转账和记录流水
        try {
            transferAccountsService.transfer(amount,payer,payee);
            return new ResponseJson(Constant.SUCCESS_CODE,"转账成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL_CODE,"操作异常,转账失败");
        }
    }

    @RequestMapping("/transByName")
    ResponseJson TransferByName(@RequestBody String data){
        //解析前端传来的信息
        Integer payerId = Integer.valueOf(JSON.parseObject(data).get("payerId").toString());
        String payeeName = JSON.parseObject(data).get("payeeName").toString();
        String payPwd = JSON.parseObject(data).get("payPwd").toString();
        BigDecimal amount = new BigDecimal(JSON.parseObject(data).get("amount").toString());

        VirtualAccount payer = transferAccountsService.searchVirById(payerId);
        VirtualAccount payee = transferAccountsService.searchVirByUserName(payeeName);

        //先进行用户信息判定
        if(payer==null) return new ResponseJson(Constant.FAIL_CODE,"当前账户异常（不存在）");
        if(payee==null) return new ResponseJson(Constant.FAIL_CODE,"对方账号不存在");
        if(payee.getPhone().equals(payer.getPhone())) return new ResponseJson(Constant.FAIL_CODE,"不可以向自己账户转账");

        String checkPayPwd = payer.getPayPwd();
        BigDecimal checkAmount = payer.getBalance();

        if(!MD5Util.getMd5(payPwd).equals(checkPayPwd)) return new ResponseJson(Constant.FAIL_CODE,"支付密码有误");
        if(amount.compareTo(BigDecimal.ZERO)<=0) return new ResponseJson(Constant.FAIL_CODE,"金额输入有误");
        if(checkAmount.subtract(amount).compareTo(BigDecimal.ZERO)<0) return new ResponseJson(Constant.FAIL_CODE,"余额不足");

        //信息无误，开始转账和记录流水
        try {
            transferAccountsService.transfer(amount,payer,payee);
            return new ResponseJson(Constant.SUCCESS_CODE,"转账成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL_CODE,"操作异常,转账失败");
        }
    }
}
